<template>
  <div class="top">
    <div class="content">
      <!-- 左侧内容 -->
      <div class="left" @click="goHome">
        <img src="../../assets/images/logo.png" alt="" />
        <p>尚医通 预约挂号统一平台</p>
      </div>
      <!-- 右侧内容 -->
      <div class="right">
        <p class="help">帮助中心</p>
        <p class="login" v-if="!userStore.userInfo.name" @click="login">
          登录/注册
        </p>
        <!-- 登录之后的用户下拉菜单 -->
        <el-dropdown v-else>
          <span class="el-dropdown-link">
            {{ userStore.userInfo.name }}
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goUser('/user/certification')"
                >实名认证</el-dropdown-item
              >
              <el-dropdown-item @click="goUser('/user/order')"
                >挂号订单</el-dropdown-item
              >
              <el-dropdown-item @click="goUser('/user/patient')"
                >就诊人管理</el-dropdown-item
              >
              <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 引入路由器
import { useRouter } from "vue-router";
import { ArrowDown } from "@element-plus/icons-vue";
// 引入用户仓库
import useUserStore from "@/store/modules/user";
// 创建仓库实例
let userStore = useUserStore();
// 获取路由器实例
let router = useRouter();
// 点击logo跳转到首页
const goHome = () => {
  router.push("/home");
};
// 点击登录弹出对话框的回调
const login = () => {
  userStore.dialogFormVisible = true;
};
// 点击退出登录的回调
const logout = async () => {
  // 请求仓库方法清空用户数据
  await userStore.logout();
  // 退出登录之后要跳转到首页
  router.push({ path: "/home" });
};
// 点击跳转到会员中心对应的二级路由的回调
const goUser = (path: string) => {
  router.push({ path: path });
};
</script>

<style scoped lang="scss">
.top {
  position: fixed;
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: center;
  background-color: #fff;
  box-shadow: 0 0 3px 3px #f1eeee;
  z-index: 999;

  .content {
    width: 1200px;
    height: 70px;
    display: flex;
    justify-content: space-between;

    .left {
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }

      p {
        font-size: 22px;
        color: #6eb3ff;
      }
    }

    .right {
      display: flex;
      justify-content: center;
      align-items: center;
      color: #9e9c9c;
      font-size: 14px;

      .help {
        margin-right: 10px;
      }
      .el-dropdown-link {
        display: flex;
        align-items: center;
        margin-top: 1px;
        font-size: 14px;
      }
    }
  }
}
</style>
